package com.chenzhengrong.demospringboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenzhengrong.demospringboot.domain.pojo.PartyOrganization;
import com.chenzhengrong.demospringboot.domain.vo.PartyTree;
import com.chenzhengrong.demospringboot.service.PartyOrganizationService;
import com.chenzhengrong.demospringboot.mapper.PartyOrganizationMapper;
import com.chenzhengrong.demospringboot.util.CustomerBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author chenzhengrong
* @description 针对表【party_organization(党组织架构表)】的数据库操作Service实现
* @createDate 2022-11-17 16:32:14
*/
@Service
@Slf4j
public class PartyOrganizationServiceImpl extends ServiceImpl<PartyOrganizationMapper, PartyOrganization>
    implements PartyOrganizationService{

    @Override
    public PartyTree listTree(String name) {
        baseMapper.selectList(null);
        return null;
    }


    /**
    * @description: 遍历树形菜单
    * @param
    * @author chenzhengrong
    * @date 2022/11/21 14:11
    */
    @Override
    public List<PartyTree> listTree() {
        List<PartyOrganization> partyOrganizations = baseMapper.selectList(null);
        List<PartyTree> partyTrees = CustomerBeanUtils.copyListProperties(partyOrganizations, PartyTree::new);
        List<PartyTree> rootCollect = partyTrees.stream()
                .filter(item -> item.getPid() == 0)
                .map(item -> {
                    //PartyTree partyTree = new PartyTree(item);
                    item.setTreeList(readTree(item, partyTrees));
                    return item;
                })
                .collect(Collectors.toList());
        return rootCollect;
    }

    public List<PartyTree> readTree(PartyTree partyTree, List<PartyTree> partyTrees) {
        List<PartyTree> trees = partyTrees.stream()
                .filter(item -> item.getPid().equals(partyTree.getId()))
                .map(item -> {
                    item.setTreeList(readTree(item, partyTrees));
                    return item;
                }).collect(Collectors.toList());
        return trees;
    }

}




